package com.msb.dongbao.portal.web.controller.studyCaptcha;


import com.msb.dongbao.common.base.annotations.TokenCheck;
import com.msb.dongbao.portal.web.code.ImageCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

@RestController
@RequestMapping("/code")
public class VerifyCodeController {

    String arrtName = "verifyCode";


    @GetMapping("/generator")
    @TokenCheck(required = false)
    public void generateCode(HttpServletRequest request, HttpServletResponse response) {
        ImageCode imageCode = ImageCode.getInstance();
        //验证码的值
        String code = imageCode.getCode();


        request.getSession().setAttribute(arrtName, code);
        //验证码图片
        ByteArrayInputStream image = imageCode.getImage();
        response.setContentType("image/jpeg");
        byte[] bytes = new byte[1024];
        try (ServletOutputStream out = response.getOutputStream()) {
            while (image.read(bytes) != -1) {
                out.write(bytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/generator-base64")
    @TokenCheck(required = false)
    public String generateCodeBase64(HttpServletRequest request, HttpServletResponse response) {

        try {
            ImageCode imageCode = ImageCode.getInstance();
            //验证码的值
            String code = imageCode.getCode();
            request.getSession().setAttribute(arrtName, code);
            //验证码图片
            ByteArrayInputStream image = imageCode.getImage();

            request.getSession().setAttribute(arrtName, code);
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[1024];
            int r = 0;
            while ((r = image.read(buff, 0, 1024)) > 0) {
                swapStream.write(buff, 0, r);
            }
            byte[] data = swapStream.toByteArray();
            return Base64.getEncoder().encodeToString(data);
        } catch (Exception e) {
            System.out.println("异常");
            return "";
        }
    }

    @GetMapping("/verify")
    @TokenCheck(required = false)
    public String verifty(String verifyCode, HttpServletRequest request) {

        String s = request.getSession().getAttribute(arrtName).toString();
        if (verifyCode.equals(s)) {
            return "验证码校验通过";
        }

        return "验证码校验不通过";
    }
}
